package com.atgm.mall.gmmallclub.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.ClubException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.Query;
import com.atgm.common.utils.R;
import com.atgm.common.vo.member.MemberInfoVo;
import com.atgm.mall.gmmallclub.dao.ArticleCommentDao;
import com.atgm.mall.gmmallclub.dao.CommentReplyDao;
import com.atgm.mall.gmmallclub.entity.ArticleCommentEntity;
import com.atgm.mall.gmmallclub.entity.CommentReplyEntity;
import com.atgm.mall.gmmallclub.feign.MemberFeignService;
import com.atgm.mall.gmmallclub.service.CommentReplyService;
import com.atgm.mall.gmmallclub.vo.resp.CommentReplyRespVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyDao, CommentReplyEntity> implements CommentReplyService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Autowired
    private ArticleCommentDao articleCommentDao;

    @Override
    public List<CommentReplyRespVo> queryReplyInfo(Map<String, Object> param) {
        List<CommentReplyRespVo> commentReplyRespVos = new ArrayList<>();

        Long commentId = Long.valueOf((String) param.get("commentId"));
        if (commentId == null) {
            throw new ClubException("入参[commentId]为空", BizCodeEnume.VAILD_EXCEPTION.getCode());
        }

        QueryWrapper<CommentReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", commentId);
        queryWrapper.orderByAsc("id");

        IPage<CommentReplyEntity> iPage = this.page(new Query<CommentReplyEntity>().getPage(param), queryWrapper);

        List<CommentReplyEntity> list = iPage.getRecords();
        if (GmMallUtils.isNotEmpty(list)) {
            List<Long> selectMemberIds = list.stream().map(CommentReplyEntity::getMemberId).collect(Collectors.toList());

            R r = memberFeignService.getMemberInfo(selectMemberIds);
            List<MemberInfoVo> memberInfoVoList = new ArrayList<>();
            if (0 == r.getCode()) {
                memberInfoVoList = r.getData(new TypeReference<List<MemberInfoVo>>() {});
            }
            List<MemberInfoVo> finalMemberInfoVoList = memberInfoVoList;
            list.forEach(commentReplyEntity -> {
                CommentReplyRespVo commentReplyRespVo = new CommentReplyRespVo();
                BeanUtils.copyProperties(commentReplyEntity, commentReplyRespVo);
                commentReplyRespVo.setHeader(finalMemberInfoVoList.stream().filter(memberInfoVo -> {
                    return memberInfoVo.getId().equals(commentReplyEntity.getMemberId());
                }).findAny().get().getHeader());
                commentReplyRespVo.setNickname(finalMemberInfoVoList.stream().filter(memberInfoVo -> {
                    return memberInfoVo.getId().equals(commentReplyEntity.getMemberId());
                }).findAny().get().getNickname());
                commentReplyRespVos.add(commentReplyRespVo);
            });
        }

        return commentReplyRespVos;
    }

    @Override
    @Transactional
    public void saveReplyInfo(CommentReplyEntity commentReplyEntity) {
        commentReplyEntity.setReplyCount(0);
        commentReplyEntity.setThumbsCount(0);
        commentReplyEntity.setDeleted(0);
        commentReplyEntity.setCreateTime(new Date());
        log.info("数据 : {}", commentReplyEntity);
        int count = baseMapper.insert(commentReplyEntity);
        if (1 != count) {
            throw new ClubException("回复失败", BizCodeEnume.CLUB_REPLY.getCode());
        }

        Long parentId = commentReplyEntity.getParentId();
        if (parentId != null) {
            baseMapper.increaseReplyCount(parentId);
        }else {
            //评论信息回复数量 +1
            count = articleCommentDao.increaseReplyCount(commentReplyEntity.getCommentId());
            if (1 != count) {
                throw new ClubException("回复失败", BizCodeEnume.CLUB_REPLY.getCode());
            }
        }



    }

    @Override
    public void updateThumbsCountInfo(Long id, Integer count) {
        QueryWrapper<CommentReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "thumbs_count");
        queryWrapper.eq("id", id);
        CommentReplyEntity commentReplyEntity = baseMapper.selectOne(queryWrapper);
        if (commentReplyEntity == null) {
            throw new ClubException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }

        if (0 == count) {  //取消点赞
            commentReplyEntity.setThumbsCount(commentReplyEntity.getThumbsCount() - 1);
        }else {
            commentReplyEntity.setThumbsCount(commentReplyEntity.getThumbsCount() + 1);
        }

        int update = baseMapper.updateById(commentReplyEntity);
        if (update != 1) {
            throw new ClubException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }
    }
}
